package com.example.bank.service.pdf;

import com.example.bank.pojo.Client;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ：Leeziqiang
 * @description：生成pdf文件
 * @date ：2021/12/11 19:41
 */
public class PDFReport {

    // 生成pdf
    public static void exportReport(Client client, HttpServletResponse response) {
        // 告诉浏览器用什么软件可以打开次此软件
        response.setHeader("content-Type", "application/pdf");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=information.pdf");

        BaseFont baseFont;
        Font font = null;
        Font font1 = null;
        Font font2 = null;
        try {
            // 创建字体
            baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                    BaseFont.NOT_EMBEDDED);
            // 使用字体
            font = new Font(baseFont, 16);
            font1 = new Font(baseFont, 18);
            font2 = new Font(baseFont, 20, Font.BOLD);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Document document = new Document();
        try {
            PdfWriter.getInstance(document, response.getOutputStream());
            document.open();

            Paragraph elements = new Paragraph("客户贷款信息", font2);
            elements.setAlignment(Paragraph.ALIGN_CENTER);
            document.add(elements);

            document.add(new Paragraph("客户姓名：" + client.getClientName(), font));
            document.add(new Paragraph("身份证号码：" + client.getClientIdNumber(), font));
            document.add(new Paragraph("车辆大驾号：" + client.getCarNumber(), font));
            document.add(new Paragraph("车辆发动机号：" + client.getEngineNumber(), font));
            document.add(new Paragraph("首次上牌时间：" + client.getRegistrationTime(), font));
            document.add(new Paragraph("行驶里程：" + client.getServiceLife(), font));
            document.add(new Paragraph("车辆现值：" + client.getPresentWorth(), font));
            document.add(new Paragraph("贷款金额：" + client.getLoanAmount(), font));
            document.add(new Paragraph("贷款年限：" + client.getLoanTerm(), font));
            document.add(new Paragraph("银行区域：" + client.getBankArea(), font));

            document.add(new Paragraph("材料证明：", font1));
            document.add(new Paragraph("户口本：", font));
            List<String> hukouben = client.getImages().get("hukouben");
            for (String name : hukouben) {
                if (name == null) {
                    break;
                }
                Image png = Image.getInstance(name);
                png.setAlignment(Image.ALIGN_CENTER);
                document.add(png);
            }
            document.add(new Paragraph("身份证：", font));
            List<String> shenfenzheng = client.getImages().get("shenfenzheng");
            for (String name : shenfenzheng) {
                if (name == null) {
                    break;
                }
                Image png = Image.getInstance(name);
                png.setAlignment(Image.ALIGN_CENTER);
                document.add(png);
            }
            document.add(new Paragraph("结婚证：", font));
            List<String> jiehunzheng = client.getImages().get("jiehunzheng");
            for (String name : jiehunzheng) {
                if (name == null) {
                    break;
                }
                Image png = Image.getInstance(name);
                png.setAlignment(Image.ALIGN_CENTER);
                document.add(png);
            }
            document.add(new Paragraph("征信：：", font));
            List<String> zhengxin = client.getImages().get("zhengxin");
            for (String name : zhengxin) {
                if (name == null) {
                    break;
                }
                Image png = Image.getInstance(name);
                png.setAlignment(Image.ALIGN_CENTER);
                document.add(png);
            }
            document.add(new Paragraph("房产证：", font));
            List<String> fangchanzheng = client.getImages().get("fangchanzheng");
            for (String name : fangchanzheng) {
                if (name == null) {
                    break;
                }
                Image png = Image.getInstance(name);
                png.setAlignment(Image.ALIGN_CENTER);
                document.add(png);
            }
            document.add(new Paragraph("行驶证：", font));
            List<String> xingshizheng = client.getImages().get("xingshizheng");
            for (String name : xingshizheng) {
                if (name == null) {
                    break;
                }
                Image png = Image.getInstance(name);
                png.setAlignment(Image.ALIGN_CENTER);
                document.add(png);
            }
            document.add(new Paragraph("车辆大照：", font));
            List<String> cheliangdazhao = client.getImages().get("cheliangdazhao");
            for (String name : cheliangdazhao) {
                if (name == null) {
                    break;
                }
                Image png = Image.getInstance(name);
                png.setAlignment(Image.ALIGN_CENTER);
                document.add(png);
            }
            document.add(new Paragraph("补充材料：", font));
            List<String> buchongcailiao = client.getImages().get("buchongcailiao");
            for (String name : buchongcailiao) {
                if (name == null) {
                    break;
                }
                Image png = Image.getInstance(name);
                png.setAlignment(Image.ALIGN_CENTER);
                document.add(png);
            }
            document.addCreationDate();
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
